﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;
using DunLibrary.Base;

namespace DunSilverlight.Views
{
    public partial class SignFileTypePage : Page
    {
        public SignFileTypePage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(SignFileTypePage_Loaded);
        }

        /// <summary>
        /// 窗体显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SignFileTypePage_Loaded(object sender, RoutedEventArgs e)
        {
            Query();//加载所有数据
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        /// <summary>
        /// 执行查询
        /// 基础资料都比较少，不需要分页，也没有查询条件
        /// </summary>
        private void Query()
        {
            //在web定义一个wcf服务，这个服务继承BaseService,然后在前台添加一个服务(如果还没有添加的话)
            SignFileTypeService.SignFileTypeServiceClient ser = new SignFileTypeService.SignFileTypeServiceClient();

            //wcf所有函数都是异步执行，所以定义并添加一个回调函数
            ser.GetListByHQLCompleted += (object sender, SignFileTypeService.GetListByHQLCompletedEventArgs e) =>
            {
                try
                {
                    int totalCount = 0;
                    //反序列化一个SignFileType的集合
                    IList<DunLibrary.Base.SignFileType> types = JsonSerializerHelper.JsonToEntities<DunLibrary.Base.SignFileType>(e.Result, out totalCount);

                    dgdSignFileType.ItemsSource = types;
                }
                catch (Exception ex)
                {
                    Tools.ShowMessage(ex.Message, "", false);
                }
            };
            //开始异步调用后台wcf服务,传入hql()
            ser.GetListByHQLAsync("from SignFileType");
        }

        /// <summary>
        /// 新增一个签收类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            SignFileType cmp =new SignFileType();

            SignFileTypeForm frmType = new SignFileTypeForm(cmp);

            Tools.OpenWindow("签收类型-新增", frmType, null);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            SignFileType cmp = dgdSignFileType.SelectedItem as SignFileType;

            if (cmp != null)
            {
                SignFileTypeForm frmControlType = new SignFileTypeForm(cmp);
                Tools.OpenWindow("签收类型-编辑", frmControlType, null);
            }
        }
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            SignFileType cmp = dgdSignFileType.SelectedItem as SignFileType;

            if (cmp != null)
            {

                MessageBoxResult mr = MessageBox.Show(string.Format("是否要删除{0}", cmp.Name), "提示", MessageBoxButton.OKCancel);

                if (mr == MessageBoxResult.OK)
                {
                    SignFileTypeService.SignFileTypeServiceClient ser = new SignFileTypeService.SignFileTypeServiceClient();

                    ser.DeleteByIdCompleted += (object sender1, SignFileTypeService.DeleteByIdCompletedEventArgs e1) =>
                    {
                        if (JsonSerializerHelper.JsonToEntity<bool>(e1.Result))
                        {
                            Tools.ShowMessage("删除成功!", "", true);
                        }
                    };

                    ser.DeleteByIdAsync(cmp.Id);
                }
            }
        }
    }
}
